<?php


namespace app\plugin\controller\article\service;


use app\common\controller\AppCommon;

class ArticleService
{
    /**
     * 初始化数据库
     * @param false $resetInstall 是否重新安装
     */
    public static function ini_sql($resetInstall = false)
    {
        if ($resetInstall) {
            AppCommon::execute('DROP TABLE IF EXISTS `' . table_name('plugin_article') . '`');
            AppCommon::execute('DROP TABLE IF EXISTS `' . table_name('plugin_article_category') . '`');
            AppCommon::execute('DROP TABLE IF EXISTS `' . table_name('plugin_article_order') . '`');
        }
        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_article') . "';")) {
            AppCommon::execute("CREATE TABLE `".table_name('plugin_article')."` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `aid` varchar(32) NOT NULL DEFAULT '' COMMENT '文章唯依标识',
  `title` varchar(255) NOT NULL DEFAULT '' COMMENT '文章标题',
  `content` text COMMENT '文章内容',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `up_time` int(10) unsigned NOT NULL DEFAULT '0',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1-展示中，0-已下线',
  `count_view` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '浏览量',
  `price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '阅读价格',
  `free_percent` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '试读比例',
  `category_id` int(11) NOT NULL DEFAULT '0' COMMENT '分类ID',
  `author` varchar(20) NOT NULL DEFAULT '' COMMENT '作者',
  `sale_count` int(10) DEFAULT '0' COMMENT '销量',
   `desc` varchar(255) NOT NULL DEFAULT '' COMMENT '文章摘要',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='付费知识文章';");
        }

        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_article_category') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_article_category') . "`  (
          `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
          `name` varchar(20) NOT NULL DEFAULT '' COMMENT '分类名称',
          `fid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上级编号',
          `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1-已启用，0-已禁用',
                  PRIMARY KEY (`id`) USING BTREE
        ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;");
        }
        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_article_order') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_article_order') . "` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `aid` varchar(32) NOT NULL DEFAULT '' COMMENT '文章唯依标识',
  `order_sn` varchar(32) NOT NULL DEFAULT '' COMMENT '订单编号',
  `uid` varchar(50) NOT NULL DEFAULT '' COMMENT '用户标识',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '-1-已取消，0-待支付，1-已支付，2-待收货，3-已完，-2-已退款',
  `price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '订单金额',
  `pay_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '实付金额',
  `add_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `up_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
  `send_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '发货时间',
  `pay_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '支付时间',
  `pay_type` varchar(20) NOT NULL DEFAULT '' COMMENT 'wechat-微信，alipay-支付宝，credit-余额',
  `trans_id` varchar(50) NOT NULL DEFAULT '' COMMENT '交易号',
  `cancel_pay_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '超时支付截止时间',
  `pay_openid` varchar(34) NOT NULL DEFAULT '' COMMENT '微信支付openid',
  `refund_total` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '累计退款',
  `ip_address` varchar(20) NOT NULL DEFAULT '' COMMENT '下单IP地址',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `uid` (`uid`),
  KEY `order_sn` (`order_sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识付费订单表';");
        }
    }
}